import { createSlice } from "@reduxjs/toolkit";

const counterStore =  createSlice({
  name: "counter",
  // 初始化state
  initialState: {
    count: 0,
  },
  // 修改状态的方法，同步方法，支持直接修改
  reducers: {
    increment: (state) => {
      state.count += 1;
    },
    decrement: (state) => {
      state.count -= 1;
    },
    addToNum (state, action) {
      state.count = action.payload;
    }
  }
})

// 解构出来的acrionCreater函数
const { increment, decrement, addToNum } = counterStore.actions;
// 获取reducer
const reducer = counterStore.reducer;
// 按需导出actionCreator
export { increment, decrement, addToNum };
// 默认导出reducer
export default reducer;